<template>
  <div class="container">
    <!-- 标题栏 -->
    <van-nav-bar title="处方套餐"
                 fixed
                 @click-left="$router.back(-1)">
      <template #left>
        <img class="icon_right"
             src="@/assets/img/down_left.png"
             alt="">
      </template>
    </van-nav-bar>
    <!-- 页面部分内容写在content盒子内 -->
    <div class="content">
      <button class="add-btn"
              @click="goAddPage">新增</button>
      <!-- 套餐列表 -->
      <div class="list">
        <div class="item"
             @click="editPackage(it)"
             v-for="(it, ins) in list"
             :key="ins">
          <div>套餐名称：{{it.name}}</div>
          <div>药物数量：{{it.drug_num}}</div>
          <van-icon class="del-icon"
                    @click.stop="delIcon(ins)"
                    color="#FFF"
                    size="5"
                    name="cross" />
        </div>
      </div>
      <div class="empty" v-if="list.length === 0">
        <img src="@/assets/home/empty.png" alt="" />
      </div>
    </div>
  </div>
</template>

<script>
import { packageList, delPackage } from '@/api/medical'
export default {
  name: '',
  data () {
    return {
      list: []
    }
  },
  created () {
    this.packageList()
  },
  methods: {
    goAddPage () {
      if (this.list.length === 5) return this.$toast.fail('套餐最多5个')
      this.$router.push('/yihu/addPackage')
    },
    // 点击套餐进行回显和编辑
    editPackage (it) {
      this.$router.push({
        path: '/yihu/addPackage',
        query: { ...it, event: 'edit' }
      })
    },
    // 删除按钮
    async delIcon (ins) {
      const { code, msg } = await delPackage({ id: this.list[ins].id })
      if (code !== 1) return this.$toast.fail(msg)
      this.$toast.success('删除成功')
      this.list.splice(ins, 1)
    },
    async packageList () {
      const { code, msg, data } = await packageList()
      if (code !== 1) return this.$toast.fail(msg)
      this.list = data
    }
  }
}
</script>

<style scoped lang="less">
.container {
  height: 100vh;
}
.empty {
  width: 100%;
  height: 230px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.icon_right {
  width: 12px;
  height: 21px;
  vertical-align: middle;
}
.content {
  padding: 20px 30px;
  padding-top: 54px;
  .add-btn {
    padding: 5px 20px;
    background: linear-gradient(180deg, #3eb3ff 0%, #436eff 100%);
    border: none;
    font-size: 16px;
    color: #ffffff;
    letter-spacing: 1px;
    border-radius: 8px;
  }
  .list {
    font-size: 12px;
    padding-top: 10px;
    .item {
      position: relative;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 20px 15px;
      box-sizing: border-box;
      border-radius: 10px;
      margin-top: 10px;
      border: 1px solid #3eb3ff;
      width: 100%;
    }
    .del-icon {
      position: absolute;
      top: -10px;
      right: -5px;
      background-color: #436eff;
      border-radius: 50%;
      padding: 5px;
    }
  }
}
</style>
